package com.qingmu.club.minaapi.custom.logic;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qingmu.club.minaapi.plus.entity.Evaluation;
import com.qingmu.club.minaapi.plus.service.EvaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.text.DecimalFormat;
import java.util.List;

@Component
public class EvaluationLogic {

    @Autowired
    EvaluationService evaluationService;


    /**
     * 查询教练的星级评价
     * @param coachId
     * @return
     */
    public String coachEvaluationStar(String coachId){

        QueryWrapper<Evaluation> qw = new QueryWrapper<>();
        qw.eq(Evaluation.COACH_ID,coachId);
        List<Evaluation> evaluationList = evaluationService.list(qw);
        if(CollectionUtils.isEmpty(evaluationList)){
            return "0";
        }
        Integer num = 0 ;
        for(Evaluation evaluation :evaluationList){
            num+=evaluation.getEvaluationStar();
        }
        String result = "";
        double starNum =(double)num/evaluationList.size();  //评价星级总数 / 总评价人数
        DecimalFormat df = new DecimalFormat("0.0");
        result = df.format(starNum);
        return result;
    }

}
